package com.hxminer.hxwebs.handle;

import com.hxminer.hxwebs.exceptions.UserException;
import com.hxminer.hxwebs.model.Result;
import com.hxminer.hxwebs.utils.ResultUitl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Author YRG
 * Created by on 2018/5/8.
 * Describe
 */
@ControllerAdvice
public class ExceptionHandle {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof UserException) {
            UserException e1 = (UserException) e;
            return ResultUitl.error(e1.getCode(), e1.getMessage());
        } else {
            logger.error("【系统异常】{}", e);
            return ResultUitl.error(-1, "未知错误!");
        }
    }
}
